package com.situ.springboot.controller;

import com.situ.springboot.mapper.UserMapper;
import com.situ.springboot.pojo.User;
import com.situ.springboot.pojo.query.UserQuery;
import com.situ.springboot.service.IUserService;
import com.situ.springboot.util.LayUITableResult;
import com.situ.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//注解
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/user/toLogin";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String name, String password, String code, HttpSession session) {
        //先判断验证码对不对，验证码正确，再判断用户名密码对不对
        //验证码不对，直接返回验证码错误，不需要再判断用户名和密码
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        User user = userService.login(name, password);
        if (user != null) {
            // Session:是一次会话，服务器会为每个用户创建一个唯一的Session
            session.setAttribute("user", user);
            return Result.ok("登录成功");
        } else {
            return Result.error("用户名或密码错误");
        }

        //return user != null ? Result.ok("登录成功") : Result.error("登录失败");
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "user_add";
    }

    //{'name':'zhansgan','password':'123','email':'123'}
    @RequestMapping("/add")
    @ResponseBody
    public Result add(User user) {
        userService.add(user);
        return Result.ok("添加成功");
    }

    // /user/deleteAll?ids=1,2,3
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deleteAll(Integer[] ids) {
        userService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        userService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/toList")
    public String toList() {
        return "user_list";
    }

    // /user/list?page=1&limit=10&name=zhang&email=3fdd
    @RequestMapping("/list")
    @ResponseBody
    public LayUITableResult list(UserQuery userQuery) {
        LayUITableResult layUITableResult = userService.list(userQuery);
        return layUITableResult;
    }

    //根据id删除
    public void deleteById() {

    }

    /*
    @RequestMapping("/list")
    public String list(Model model) {
        List<User> list = userService.list();
        model.addAttribute("list", list);
        //代表转发到user_list.html这个页面
        return "user_list";
    }

    // [{'id':1,'name':'zhangsan'},{}]
    // http://127.0.0.1:8080/user/list
    // http://localhost:8080/user/list
    @RequestMapping("/list1")
    @ResponseBody  //返回json格式数据
    public List<User> list1() {
        *//*User user1 = new User();
        user1.setId(1);
        user1.setName("zhangsan");
        user1.setAge(23);
        User user2 = new User();
        user2.setId(2);
        user2.setName("lisi");
        user2.setAge(24);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);*//*

        List<User> list = userService.list();
        return list;
    }*/
}
